﻿using UnityEngine;
using System.Collections;
using System;

public class JohnnyUIRepeater : MonoBehaviour {

	public enum RowDirection
	{
		XMinus,
		X,
		YMinus,
		Y,
		ZMinus,
		Z
	}

	public RowDirection RepeaterRowDirection;

	public AlarmTimeInfo[] Rows = null;

	public int RowsPerPage = 6;
	public int TotalRecords {
		get {
			return Rows.Length;
		}
	}
	public float SpaceBetweenRows = 0.1f;
	public int CurrentPage = 1;
	public JohnnyUIButton NextPageButton = null;
	public JohnnyUIButton PreviousPageButton = null;

	public GameObject TemplateObject = null;
	bool shown = false;
	

	public int TotalPages {
		get {
			int floor = TotalRecords / RowsPerPage;
			int last = (TotalRecords % RowsPerPage > 0) ? 1 : 0;
			return floor + last;
		}
	}

	// Use this for initialization
	void Start () {
		this.Rows = new AlarmTimeInfo[] {
			new AlarmTimeInfo() {
				AMorPM = AlarmTimeInfo.AMPM.AM,
				Minute = 30,
				Hour = 5,
				Days = new DayOfWeek[] {
					DayOfWeek.Sunday,
					DayOfWeek.Saturday
				}
			},
			new AlarmTimeInfo() {
				AMorPM = AlarmTimeInfo.AMPM.AM,
				Minute = 0,
				Hour = 8,
				Days = new DayOfWeek[] {
					DayOfWeek.Monday,
					DayOfWeek.Tuesday,
					DayOfWeek.Wednesday,
					DayOfWeek.Thursday,
					DayOfWeek.Friday
				}
			}
		};

		this.ShowRepeater();
	}

	void ShowRepeater() {
		//TemplateObject.gameObject.SetActive(true);
		//TemplateObject.gameObject.SetActive(false);
		//this.TemplateObject.gameObject.SetActive(true);
		Vector3 startPosition = new Vector3(0f,0f,0f);

		foreach(AlarmTimeInfo time in this.Rows) {
			Transform myTrans = this.TemplateObject.transform;

			Transform newObject = (Transform)Instantiate(myTrans, startPosition, myTrans.rotation);
			newObject.transform.parent = this.transform;
			newObject.transform.localPosition = startPosition;
			AlarmTimeInfo info = (AlarmTimeInfo)newObject.GetComponent(typeof(AlarmTimeInfo));
			info.Hour = time.Hour;
			info.Minute = time.Minute;
			info.Days = time.Days;
			info.AMorPM = time.AMorPM;
			
			startPosition = this.IncrementDirection(startPosition);
		}
		//this.TemplateObject.gameObject.SetActive(false);
	}

	Vector3 IncrementDirection(Vector3 input) {
		switch(this.RepeaterRowDirection) {
		case RowDirection.X:input.x += this.SpaceBetweenRows; break;
		case RowDirection.XMinus:input.x -= this.SpaceBetweenRows;break;
		case RowDirection.Y:input.y += this.SpaceBetweenRows;break;
		case RowDirection.YMinus:input.y -= this.SpaceBetweenRows;break;
		case RowDirection.Z:input.z += this.SpaceBetweenRows;break;
		case RowDirection.ZMinus:input.z -= this.SpaceBetweenRows;break;
		default:break;
		}
		return input;
	}
	
	// Update is called once per frame
	void Update () {
		/*
		if (!this.shown) {
			Debug.Log("Here");
			this.shown = true;
			this.ShowRepeater();
		}
		*/
	}
}
